package com.renren.project.admin;

import com.renren.project.core.utils.DateUtils;
import com.renren.project.core.utils.JwtUtils;
import com.renren.project.core.utils.RsaUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JwtTestOne {
    @Autowired
    private JwtUtils jwtUtils;

	private static final String pubKeyPath = "D:\\project-0722\\rsa\\rsa.pub";
    private static final String priKeyPath = "D:\\project-0722\\rsa\\rsa.pri";

    private PublicKey publicKey;

    private PrivateKey privateKey;

    @Test
    public void testRsa() throws Exception {
        RsaUtils.generateKey(pubKeyPath, priKeyPath, "X-Litemall-Token");
    }

    @Before
    public void testGetRsa() throws Exception {
        this.publicKey = RsaUtils.getPublicKey();
        this.privateKey = RsaUtils.getPrivateKey();
    }

    @Test
    public void testGenerateToken() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("id", "11");
        map.put("username", "liuyan");
        // 生成token
        String token = jwtUtils.generateToken(map, privateKey,  DateUtils.getAfterDate(new Date(), 0, 0, 0, 720, 0, 0));
        System.out.println("token = " + token);
    }

    @Test
    public void testParseToken() throws Exception {
        String token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ0aGlzIGlzIGxpdGVtYWxsIHRva2VuIiwiYXVkIjoiTUlOSUFQUCIsImlzcyI6IkxJVEVNQUxMIiwiaWQiOiIxMSIsImV4cCI6MTYwMDkxNzQ1NiwidXNlcm5hbWUiOiJsaXV5YW4ifQ.Jc98ZKjbGZOQ8iJpdTkzvUmxIS2uTTI7Y1XvnSxlHWG5zsnRgycDsZeIQRVZAG8C8ejxwvE6dnCkPy2-x2mUTcJ_YxrAsf3YQ1KN9IR9zk8Kdl9BQu5Vbpv2-gTORObNT5gizkEG8oo0MUFWHO4u2kTacQwQ1VlJKg0yej0XZr9MSMFQRYQPElSmVIs3SwYoU1GCZgTaVoN6_wyy6rVhjkg4zSlHUWlylGjFuGK6Eg_1B0HdtZUbKLTCd5EEHqqi4rYco2ak8t9-b7xZR4btysK4yvML68uMILbWiX_VcnJrKeoWZakIGIOHtNH53elokJqv-Lh9vTqzfeVMfPWzLA";

        // 解析token
        Map<String, Object> map = jwtUtils.getInfoFromToken(token, publicKey);
        System.out.println("id: " + map.get("id"));
        System.out.println("userName: " + map.get("username"));
    }
}